package com.tenda.sonicdemo;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * Created by DongHao on 2017/8/23.
 * Description:
 */

public class FileUtils {

    public static void copyAssetsToSDCard(Context context, String oldPath, String newPath) {
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            String[] fileNames = context.getAssets().list(oldPath);//获取assets目录下的所有文件及目录名
            if (fileNames.length > 0) {//如果是目录
                File file = new File(newPath);
                file.mkdirs();//如果文件夹不存在，则递归
                for (String fileName : fileNames) {
                    copyAssetsToSDCard(context, oldPath + "/" + fileName, newPath + "/" + fileName);
                }
            } else {//如果是文件
                is = context.getAssets().open(oldPath);
                fos = new FileOutputStream(new File(newPath));
                byte[] buffer = new byte[1024];
                int byteCount = 0;
                while ((byteCount = is.read(buffer)) != -1) {//循环从输入流读取 buffer字节
                    fos.write(buffer, 0, byteCount);//将读取的输入流写入到输出流
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.flush();//刷新缓冲区
                    fos.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static String getHtmlPath(Context context, String endPath) {
        String[] strs = endPath.split("\\?");
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/zgbHtml" + strs[0]);
        if (file.exists()) {
            return "file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/zgbHtml" + endPath;
        } else {
            return "file:///android_asset/zgbHtml" + endPath;
        }
    }

    /**
     * 解压zip文件
     *
     * @throws Exception
     */
    public static void UnZipFolder() throws Exception {
        String zipFileString = Environment.getExternalStorageDirectory().getAbsolutePath() + "/zgbFile/zgbHtml.zip";
        String outPathString = Environment.getExternalStorageDirectory().getAbsolutePath() + "/zgbHtml";
        ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
        ZipEntry zipEntry;
        String szName = "";
        while ((zipEntry = inZip.getNextEntry()) != null) {
            szName = zipEntry.getName();
            if (zipEntry.isDirectory()) {
                // get the folder name of the widget
                szName = szName.substring(0, szName.length() - 1);
                File folder = new File(outPathString + File.separator + szName);
                if (!folder.exists()){
                    folder.mkdirs();
                }
            } else {
                File file = new File(outPathString + File.separator + szName);
                file.createNewFile();
                // get the output stream of the file
                FileOutputStream out = new FileOutputStream(file);
                int len;
                byte[] buffer = new byte[1024];
                // read (len) bytes into buffer
                while ((len = inZip.read(buffer)) != -1) {
                    // write (len) byte from buffer at the position 0
                    out.write(buffer, 0, len);
                    out.flush();
                }
                out.close();
            }
        }
        inZip.close();
    }
}
